package com.example.orderfoodsystem22201310liuziming.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/UserLogin")
public class UserLoginController {
    @Autowired
    private HttpSession httpSession;

    @Value("${bookingsystem.username}") //注入
    private String username;
    @Value("${bookingsystem.userpassword}")
    private String userpassword;

    //---------------------------------------------------------------------------------
    @RequestMapping("/login")
    protected String login(Model model) {
        model.addAttribute("wrong", "");
        return "crm/UserLogin";
    }

    @RequestMapping("/loginCheck")
    protected String loginCheck(Model model, @RequestParam("username") String usernameCheck, @RequestParam("userpassword") String passwordChecck) {
        if (usernameCheck.equals(this.username) && passwordChecck.equals(this.userpassword)) {
            httpSession.setAttribute("loggedId","success");
            httpSession.setAttribute("who",username);
            return "seller/SellerIndex";
        } else {
            model.addAttribute("wrong", "账户密码出错");
            return "crm/UserLogin";
        }
    }

    @RequestMapping("/")
    protected String pass() {
        if ("success".equals(httpSession.getAttribute("loggedId")))
            return "seller/SellerIndex";
        else
            return"redirect:/UserLogin/login";
    }

//----------------------------------------------------------------------------

    @RequestMapping("/setsession")
    @ResponseBody
    protected String setSesstion() {
        httpSession.setAttribute("yonghu", username);
        return "set ok";
    }

    @RequestMapping("/getsession")
    @ResponseBody
    protected String getSesstion() {
        httpSession.getAttribute("yonghu");
        return "get ok";
    }

}
